class Solution:
    def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
        if not s1 or not s2:
            return s3 == (s1 + s2)
        dongtai_list = [[False for j in range(len(s2) + 1)][:] for i in range(len(s1) + 1)]
        dongtai_list[0][0] = True
        for i in range(1, len(s1) + 1):
            dongtai_list[i][0] = (s1[:i] == s3[:i])
        for j in range(1, len(s2) + 1):
            dongtai_list[0][j] = (s2[:j] == s3[:j])
        for i in range(1, len(s1) + 1):
            for j in range(1, len(s2) + 1):
                dongtai_list[i][j] = (dongtai_list[i-1][j] and s1[i-1] == s3[i+j-1]) or \
                                     (dongtai_list[i][j-1] and s2[j-1] == s3[i+j-1])
        return dongtai_list[-1][-1]


a = Solution()
print(a.isInterleave(s1="aabcc", s2="dbbca", s3="aadbbcbcac"))
print(a.isInterleave(s1="aabcc", s2="dbbca", s3="aadbbbaccc"))
